clear all
*Define directory
cap cd "...\Replication pacakage\"

******************************************************
clear all
use data.dta

gen TotalBPDL= ApprovedAmountRealEstate+ ApprovedAmountContent
destring Year, replace force
keep if Year >2002

************************************************************
gen BPDLpct=TotalBPDL/25000
replace BPDLpct=TotalBPDL/14000 if Year<=2013
replace BPDLpct=TotalBPDL/14000 if Year<=2015 & FEMADisasterNumber ==""
replace BPDLpct=TotalBPDL/10000 if Year<=2007

gen sample=3 //sample 3: 2014-2020
replace sample=2 if Year<=2013
replace sample=2 if Year<=2015 & FEMADisasterNumber =="" //sample 2: 2008-2013
replace sample=1 if Year<=2007 //sample 1: 2003-2007

keep if TotalBPDL>0 & TotalBPDL<65000 &TotalVerifiedLoss>0 & TotalVerifiedLoss<65000

scatter TotalVerifiedLoss TotalBPDL  if sample==1, xsc(r(0 65000)) ysc(r(0 65000)) msize(vsmall) xline(10000)  ylabel(,  nogrid) graphregion(color(white)) xtitle( "Loan amount")
graph export "Figure 4\Figure 4a_loss_03-07.pdf", as(pdf) replace

scatter TotalVerifiedLoss TotalBPDL  if sample==2, xsc(r(0 65000)) ysc(r(0 65000)) msize(vsmall) xline(14000)  ylabel(,  nogrid) graphregion(color(white)) xtitle( "Loan amount")
graph export "Figure 4\Figure 4c_loss_08-13.pdf", as(pdf) replace

scatter TotalVerifiedLoss TotalBPDL  if sample==3, xsc(r(0 65000)) ysc(r(0 65000)) msize(vsmall) xline(25000)  ylabel(,  nogrid) graphregion(color(white)) xtitle( "Loan amount")
graph export "Figure 4\Figure 4e_loss_14-20.pdf", as(pdf) replace

*2003-2007 Sample

twoway__histogram_gen TotalBPDL  if sample==1, w(500) start(1)  percent generate(h11 x11, replace)
twoway__histogram_gen TotalVerifiedLoss  if sample==1, w(500) start(1)  percent generate(h12 x12, replace)
twoway (line h11 x11, lp(l)) (line h12 x12, lp(-)), ylabel(0 (2) 12,  nogrid) graphregion(color(white)) legend(row(1) label(1 "Loan amount") label(2 "Total Verified Loss")) ytitle( "Percentage of Loans") xline(10000,lp(-) lc(black))
graph export "Figure 4\Figure 4b_loss_line_03-07.pdf", as(pdf) replace

*2008-2013 Sample

twoway__histogram_gen TotalBPDL  if sample==2, w(500) start(1)  percent generate(h21 x21, replace)
twoway__histogram_gen TotalVerifiedLoss  if sample==2, w(500) start(1)  percent generate(h22 x22, replace)
twoway (line h21 x21, lp(l)) (line h22 x22, lp(-)), ylabel(0 (2) 12,  nogrid) graphregion(color(white)) legend(row(1) label(1 "Loan amount") label(2 "Total Verified Loss")) ytitle( "Percentage of Loans") xline(14000,lp(-) lc(black))
graph export "Figure 4\Figure 4d_loss_line_08-13.pdf", as(pdf) replace

*2014-2020 Sample

twoway__histogram_gen TotalBPDL  if sample==3, w(500) start(1)  percent generate(h31 x31, replace)
twoway__histogram_gen TotalVerifiedLoss  if sample==3, w(500) start(1)  percent generate(h32 x32, replace)
twoway (line h31 x31, lp(l)) (line h32 x32, lp(-)), ylabel(0 (2) 12,  nogrid) graphregion(color(white)) legend(row(1) label(1 "Loan amount") label(2 "Total Verified Loss")) ytitle( "Percentage of Loans") xline(25000,lp(-) lc(black))

graph export "Figure 4\Figure 4f_loss_line_14-20.pdf", as(pdf) replace
